<template>
  <!-- 新增修改弹框 -->
  <el-dialog :title="title" :visible.sync="dialogShow" @close="closeDialog">
    <!-- 绘制表单 -->
    <el-form
      ref="form"
      :model="formData"
      :rules="formRules"
      label-width="160px"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width: 440px"></el-input>
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="formData.code"
          style="width: 440px"
          placeholder="请选择"
        ></el-input>
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          @focus="getEmployeeData"
          v-model="formData.manager"
          style="width: 440px"
          placeholder="请选择"
        >
          <el-option
            v-for="(item, index) in managerList"
            :key="index"
            :label="item.username"
            :value="item.username"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="formData.introduce"
          type="textarea"
          rows="3"
          style="width: 440px"
        ></el-input>
      </el-form-item>
      <el-row type="flex" justify="center">
        <el-button size="small" type="primary" @click="btnOk">确定</el-button>
        <el-button size="small" @click="dialogShow = false">取消</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import {
  getDepartmentListApi,
  addDepartmentApi,
  getDepartmentInfoApi,
  editDepartmentApi,
} from "@/api/departments";
import { getEmployeeSimpleApi } from "@/api/employees";
export default {
  computed: {
    title() {
      return this.formData.id ? "修改" : "新增";
    },
  },
  data() {
    //部门名称重复的校验
    var checkNameRepeat = async (rule, value, callback) => {
      //1.查询所有部门,2.将当前兄弟部门选出,3.查重,4.校验不通过提示
      let { depts } = await getDepartmentListApi();
      let arr = depts.filter((item) => item.pid === this.formData.pid);
      //如果是编辑状态下， 还要排除自己，否则会重复
      if (this.formData.id) {
        arr = arr.filter((item) => item.id !== this.formData.id);
      }
      const isRepeat = arr.some((item) => item.name === value);
      isRepeat ? callback(new Error("名称重复了")) : callback();
    };
    //部门编码重复的校验
    var checkCodeRepeat = async (rule, value, callback) => {
      //1.查询所有部门,3.查重,4.校验不通过提示
      let { depts } = await getDepartmentListApi();
      //如果是编辑状态下， 还要排除自己，否则会重复
      if (this.formData.id) {
        depts = depts.filter((item) => item.id !== this.formData.id);
      }
      const isRepeat = depts.some((item) => item.code === value);
      isRepeat ? callback(new Error("编码重复了")) : callback();
    };
    return {
      dialogShow: false,
      formData: {
        code: "",
        introduce: "",
        manager: "",
        name: "",
        pid: "",
      },
      formRules: {
        name: [
          { required: true, message: "请输入", trigger: "blur" },
          { validator: checkNameRepeat, trigger: "blur" },
        ],
        code: [
          { required: true, message: "请输入", trigger: "blur" },
          { validator: checkCodeRepeat, trigger: "blur" },
        ],
        manager: [{ required: true, message: "请输入", trigger: "blur" }],
        introduce: [{ required: true, message: "请输入", trigger: "blur" }],
      },
      managerList: [], // 负责人数据
    };
  },
  methods: {
    // 获取负责人的数据
    async getEmployeeData() {
      this.managerList = await getEmployeeSimpleApi();
    },
    // 提交事件逻辑
    async btnOk() {
      // 1 验证表单校验是否通过
      // 2 发送请求
      // 3 提示成功
      // 4 更新列表
      // 5 关闭弹框 重置表单
      try {
        await this.$refs.form.validate();
      } catch (error) {
        return console.log(error);
      }
      //判断是修改还是新增
      if (this.formData.id) {
        //修改
        await editDepartmentApi(this.formData);
        this.$message.success("修改成功");
      } else {
        // 新增
        await addDepartmentApi(this.formData);
        this.$message.success("添加成功");
      }

      this.$emit("updateList");
      this.dialogShow = false; // 关闭弹框会触发重置表达的逻辑
    },
    // 关闭弹框
    closeDialog() {
      // 重置表单的逻辑
      this.formData = {
        code: "",
        introduce: "",
        manager: "",
        name: "",
        pid: "",
      };
      // 移除校验
      this.$refs.form.clearValidate();
    },
    // 编辑  表单数据回显
    async getFormDataInfo(id) {
      this.formData = await getDepartmentInfoApi(id);
      this.dialogShow = true;
    },
  },
};
</script>
<style></style>
